package com.zlt.bkparking.controller.parking;

import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.CouponDTO;
import com.zlt.bkparking.service.CouponService;
import com.zlt.bkparking.vo.CouponVO;
import com.zlt.bkparking.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname CouponController
 * @Description TODO
 * @Date 2022/10/5 13:18
 * @Created by ${Lijijun}
 */
@RequestMapping("coupon")
@RestController
public class CouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping("add")
//    @CrossOrigin
    public Result addCoupon(/*@RequestBody*/ CouponDTO couponDTO){
        boolean bool = false;
        try {
            bool = couponService.insertCoupon(couponDTO);
            return bool?Result.ok("添加优惠券成功"):Result.error("添加优惠券失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

//    @GetMapping("all")
    @PostMapping("all")
//    @CrossOrigin
    public Result allCoupon(CouponDTO couponDTO){
//        System.out.println("CouponDTO====>"+couponDTO);
        PageInfo<CouponVO> couponVOPageInfo = null;
        try {
            couponVOPageInfo = couponService.selectCoupons(couponDTO);

            return Result.ok("当前停车场的优惠券查询成功",couponVOPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage(),couponVOPageInfo);
        }
    }

    @PostMapping("update")
//    @CrossOrigin
    public Result editPKCoupon(/*@RequestBody*/ CouponDTO couponDTO){
        System.out.println("couponDTO====>"+couponDTO);
        Boolean bool = null;
        try {
            bool = couponService.updateCoupon(couponDTO);
            return bool?Result.ok("停车场优惠券信息更新成功"):Result.error("停车场优惠券信息更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }


    }
}
